﻿// VsPkg.cs : Implementation of VisualStudio
//

using System;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using System.ComponentModel.Design;
using Microsoft.Win32;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell;
using VSLangProj80;
using EnvDTE;
using System.Windows.Forms;
using NBusiness.VisualStudio.Generators;

namespace NBusiness.VisualStudio
{
    [PackageRegistration(UseManagedResourcesOnly = true)]
    [DefaultRegistryRoot("Software\\Microsoft\\VisualStudio\\9.0Exp")]
    [InstalledProductRegistration(false, "#110", "#112", "1.0", IconResourceID = 400)]
    [ProvideLoadKey("Standard", "3.0", "NBusiness", "NBusiness", 1)]
    [Guid(GuidList.guidNBusinessPkgString)]
    [ProvideServiceAttribute(typeof(ESharpLanguageService), ServiceName = "Visual E#")]
    [ProvideLanguageServiceAttribute(
         typeof(ESharpLanguageService),
         "Visual E#",
         106,                          // resource ID of localized language name
         CodeSense = true,             // Supports IntelliSense
         RequestStockColors = false,   // Supplies custom colors
         EnableCommenting = true,      // Supports commenting out code
         EnableAsyncCompletion = true  // Supports background parsing
    )]
    [ProvideLanguageExtensionAttribute(typeof(ESharpLanguageService), ".es")]
    [ProvideLanguageCodeExpansionAttribute(
         typeof(ESharpLanguageService),
         "Visual E#", // Name of language used as registry key.
         106,           // Resource ID of localized name of language service.
         "esharp",  // language key used in snippet templates.
         @"%InstallRoot%\ESharp\SnippetsIndex.xml",  // Path to snippets index
         SearchPaths = @"%InstallRoot%\ESharp\Snippets\%LCID%\Snippets\;" +
                       @"%MyDocs%\Code Snippets\ESharp\My Code Snippets"
    )]
    #region Code Generators
    [ProvideObject(typeof(ESharpCodeGenerator))]
    [CodeGeneratorRegistration(typeof(ESharpCodeGenerator), ".es", vsContextGuids.vsContextGuidVCSProject, GeneratesDesignTimeSource = true)]
    [CodeGeneratorRegistration(typeof(ESharpCodeGenerator), ".es", vsContextGuids.vsContextGuidVBProject, GeneratesDesignTimeSource = true)]
    [CodeGeneratorRegistration(typeof(ESharpCodeGenerator), ".es", vsContextGuids.vsContextGuidVJSProject, GeneratesDesignTimeSource = true)]
    
    [ProvideObject(typeof(SparkTemplateGenerator))]
    [CodeGeneratorRegistration(typeof(SparkTemplateGenerator), ".spark", vsContextGuids.vsContextGuidVCSProject, GeneratesDesignTimeSource = true)]
    [CodeGeneratorRegistration(typeof(SparkTemplateGenerator), ".spark", vsContextGuids.vsContextGuidVBProject, GeneratesDesignTimeSource = true)]
    [CodeGeneratorRegistration(typeof(SparkTemplateGenerator), ".spark", vsContextGuids.vsContextGuidVJSProject, GeneratesDesignTimeSource = true)]
    #endregion    
    [ProvideMenuResource(1000, 1)]
    [ProvideObject(typeof(ESharpScanner))]
    public sealed class ESharpPackage : Package
    {
        /// <summary>
        /// Default constructor of the package.
        /// Inside this method you can place any initialization code that does not require 
        /// any Visual Studio service because at this point the package object is created but 
        /// not sited yet inside Visual Studio environment. The place to do all the other 
        /// initialization is the Initialize method.
        /// </summary>
        public ESharpPackage()
        {
            Trace.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering constructor for: {0}", this.ToString()));
        }

        /////////////////////////////////////////////////////////////////////////////
        // Overriden Package Implementation
        #region Package Members

        /// <summary>
        /// Initialization of the package; this method is called right after the package is sited, so this is the place
        /// where you can put all the initilaization code that rely on services provided by VisualStudio.
        /// </summary>
        protected override void Initialize()
        {
            Trace.WriteLine (string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this.ToString()));
            base.Initialize();

            IServiceContainer serviceContainer = this as IServiceContainer;
            DTE dte = GetService(typeof(DTE)) as DTE;
            ESharpLanguageService langService = new ESharpLanguageService(dte);
            langService.SetSite(this);

            serviceContainer.AddService(typeof(ESharpLanguageService),
                                        langService,
                                        true);

            //This is a hack but there appears to be no other way for certain classes to get ahold of this.
            ESharpLanguageService.Current = langService;

            // Add our command handlers for menu (commands must exist in the .vsct file)
            OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
            if (null != mcs)
            {
                // Create the command for the menu item.
                CommandID menuCommandGenerateFromDatabase = new CommandID(GuidList.guidNBusinessCmdSet, (int)PkgCmdIDList.cmdidGenerateFromDatabase);
                MenuCommand menuItemGenerateFromDatabase = new MenuCommand(GenerateFromDatabaseCallback, menuCommandGenerateFromDatabase);
                mcs.AddCommand(menuItemGenerateFromDatabase);
            }
        }
        #endregion

        #region MenuItem callbacks
        /// <summary>
        /// This function is the callback used to execute a command when the a menu item is clicked.
        /// See the Initialize method to see how the menu item is associated to this function using
        /// the OleMenuCommandService service and the MenuCommand class.
        /// </summary>
        private void GenerateFromDatabaseCallback(object sender, EventArgs e)
        {
            MessageBox.Show("Coming soon!");
        }
        #endregion
    }

    static class PkgCmdIDList
    {
        public const uint cmdidGenerateFromDatabase = 0x100;
    };
}